﻿using System;
using System.Collections;
using System.Collections.Generic;

public delegate void DataCenterHandler(string key, Object value, Object lastValue);

public class DataCenter
{
	private Dictionary<string, DataItem> dict = new Dictionary<string, DataItem>();

	public void Write(string key, Object value)
	{
		if (!dict.ContainsKey(key))
			dict[key] = new DataItem(key, value);
		else
			dict[key].Value = value;
	}

	public T Read<T>(string key)
	{
		T value;
		if (dict.ContainsKey(key))
			return value = (T)dict[key].Value;
		return default(T);
	}

	public void Remove(string key)
	{
		dict.Remove(key);
	}

	public void AddListener(string key, DataCenterHandler handler)
	{
		if (!dict.ContainsKey(key))
			dict[key] = new DataItem(key, 0);

		dict[key].listeners += handler;
	}

	public void RemoveListener(string key, DataCenterHandler handler)
	{
		if (dict.ContainsKey(key))
			dict[key].listeners -= handler;
	}
}
